package com.allen.app.common.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;


/**
*
* Description: This is a 检查对象为空工具类
*
* @author Limiaojun
* @date 2020/1/9 10:23
**/
@Slf4j
@UtilityClass
public class CheckObjectUtils {

    /**
    * 全部不为空.
    * @param obj  传入可变对象
    * @return boolean 
    * @author Limiaojun
    * @date 2019/11/10 16:00
    */ 
    public boolean allNonNull(Object... obj) {
        obj = Objects.isNull(obj) ? new Object[]{null} : obj;
        for (Object o : obj) {
            if (isNull(o)) {
                return false;
            }
        }
        return true;
    }

    /** 
    * 至少一个不为空.
    * @param obj  传入可变对象
    * @return boolean 
    * @author Limiaojun
    * @date 2019/11/10 16:00
    */ 
    public boolean oneNonNull(Object... obj) {
        obj = Objects.isNull(obj) ? new Object[]{null} : obj;
        for (Object o : obj) {
            if (!isNull(o)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 全部为空.
     * @param obj 传入可变对象
     * @return boolean
     * @author Limiaojun
     * @date 2019/11/10 16:00
     */
    public boolean allIsNull(Object... obj) {
        obj = Objects.isNull(obj) ? new Object[]{null} : obj;
        for (Object o : obj) {
            if (!isNull(o)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 至少一个为空.
     * @param obj 传入可变对象
     * @return boolean
     * @author Limiaojun
     * @date 2019/11/10 16:00
     */
    public boolean oneIsNull(Object... obj) {
        obj = Objects.isNull(obj) ? new Object[]{null} : obj;
        for (Object o : obj) {
            if (isNull(o)) {
                return true;
            }
        }
        return false;
    }


    /** 
    * This is a nonNull.
    * @param obj  传入对象
    * @return boolean 
    * @author Limiaojun
    * @date 2019/11/10 16:01
    */ 
    public boolean nonNull(Object obj) {
        return !isNull(obj);
    }

    /** 
    * This is a isNull.
    * @param obj  传入对象
    * @return boolean 
    * @author Limiaojun
    * @date 2019/11/10 16:01
    */ 
    public boolean isNull(Object obj) {
        if (Objects.isNull(obj)) {
            return true;
        } else if (obj instanceof String) {
            return StrUtil.isBlank((String) obj);

        } else if (obj instanceof Integer) {
            return (Integer) obj == 0;

        } else if (obj instanceof Float) {
            return (Float) obj == 0f;

        } else if (obj instanceof Double) {
            return (Double) obj == 0D;

        } else if (obj instanceof Long) {
            return (Long) obj == 0L;

        } else if (obj instanceof Object[]) {
            return ((Object[]) obj).length == 0;

        } else if (obj instanceof Collection) {
            //noinspection rawtypes
            return CollectionUtils.isEmpty((Collection) obj);

        } else if (obj instanceof Map) {
            //noinspection rawtypes
            return CollectionUtils.isEmpty((Map) obj);

        } else if (obj instanceof Optional) {
            //noinspection rawtypes
            return !((Optional) obj).isPresent();
        }
        return false;
    }

    public boolean objIsNull(Object o){
        return Objects.isNull(o);
    }

    /**
     * 匹配字符串数组是否包含指定完整字符串.
     *
     * @param strArr 字符串数组,逗号分割
     * @param str 字符串
     * @return boolean
     * @author Limiaojun
     * @date 2021/3/12 0012 09:52
     */
    public boolean containsStr(String strArr,String str){
        Assert.notNull(strArr,"待匹配字符串数组 [strArr] 不能为空");
        str = StrUtil.nullToEmpty(str);
        strArr = strArr.replaceAll(",","|");

        return Pattern.matches(strArr, str);
    }

//    public static void main(String[] args) {
//        System.out.println(containsStr("abc_,ab","abc"));
//        System.out.println(oneNonNull(0,1));
//        System.out.println(allNonNull(1,1));
//        System.out.println(oneIsNull(2,0));
//        System.out.println(allIsNull(0,0));
//        System.out.println(String.format("Object>>> :\t %s", isNull(null)));
//        System.out.println(String.format("STRING>>> :\t %s", isNull("")));
//        System.out.println(String.format("Integer>>> :\t %s", isNull(0)));
//        System.out.println(String.format("Float>>> :\t %s", isNull(0.0)));
//        System.out.println(String.format("Double>>> :\t %s", isNull(0.00)));
//        System.out.println(String.format("ARRAY>>> :\t %s", isNull(new String[]{})));
//        System.out.println(String.format("listIsNull>>> :\t %s", isNull(Lists.newArrayList())));
//        System.out.println(String.format("mapIsNull>>> :\t %s", isNull(Maps.newHashMap())));
//        System.out
//            .println(String.format("optionalIsNull>>> :\t %s", isNull(Lists.newArrayList().stream().findFirst())));
//    }


}
